<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="dynamicTitle">
        <el-input v-model="queryParams.dynamicTitle" placeholder="请输入标题" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="编者" prop="dynamicFeed">
        <el-input v-model="queryParams.dynamicFeed" placeholder="请输入编者" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="发布时间" prop="pushTime">
        <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="分类" prop="dynamicTypeId">
        <el-select v-model="queryParams.dynamicTypeId" placeholder="请选择类别" @change="handleQuery" style="width: 100%">
          <el-option v-for="dict in dynamicTypeList" :key="dict.dynamicTypeId" :label="dict.dynamicTypeName"
            :value="dict.dynamicTypeId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="发布状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择发布状态" clearable>
          <el-option v-for="dict in dict.type.publish_status" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['bussiness:dynamic:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['bussiness:dynamic:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['bussiness:dynamic:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['bussiness:dynamic:export']">导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-back" size="mini" @click="handleBack"
          v-hasPermi="['bussiness:dynamic:back']">撤回</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dynamicList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--      <el-table-column label="编号" align="center" prop="dynamicId" />-->
      <!--      <el-table-column label="排序" align="center" prop="orderNum" width="60" />-->
      <el-table-column label="分类" align="center" prop="dynamicTypeName">
        <template slot-scope="scope">
          <div style="
              background: #4d3000;
              width: 50px;
              height: 50px;
              padding: 3px 5px 5px 5px;
              text-align: center;
              margin: auto;
              border-radius: 5px;
            ">
            <span style="
                font-size: 14px;
                color: #ffffff;
                line-height: 14px;
                margin: 0;
                padding: 0;
                text-overflow: ellipsis;
              ">
              {{ scope.row.dynamicTypeName }}
            </span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="标题" align="left" prop="dynamicTitle">
        <template slot-scope="scope">
          <span style="color: red" v-if="scope.row.isTop == '1'">[顶] </span>
          <span style="color: red" v-if="scope.row.isBottom == '1'">[底] </span>
          <span>{{ scope.row.dynamicTitle }}</span>
        </template>
      </el-table-column>
      <el-table-column label="编者" align="center" prop="dynamicFeed" />
      <el-table-column label="发布时间" align="center" prop="pushTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.pushTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column label="附件" align="center" prop="files" />-->
      <el-table-column label="发布状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.publish_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-edit"-->
          <!--            @click="handleUpdate(scope.row)"-->
          <!--            v-hasPermi="['bussiness:dynamic:edit']"-->
          <!--          >修改</el-button>-->

          <el-button size="mini" style="width: 32px" type="primary" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:list:edit']">修改</el-button>
          <el-tooltip class="item" effect="light" content="上移" placement="top">
            <el-button size="mini" type="primary" icon="el-icon-top" @click="moveUpOrDown(scope.row, 'up')"
              v-hasPermi="['system:list:remove']"></el-button>
          </el-tooltip>
          <el-tooltip class="item" effect="light" content="下移" placement="top">
            <el-button size="mini" type="primary" icon="el-icon-bottom" @click="moveUpOrDown(scope.row, 'down')"
              v-hasPermi="['system:list:remove']"></el-button>
          </el-tooltip>
          <el-tooltip class="item" effect="light" :content="scope.row.isTop != '1' ? '置顶' : '取消置顶'" placement="top">
            <el-button size="mini" type="primary"
              :icon="scope.row.isTop != '1' ? 'el-icon-upload2' : 'el-icon-refresh-left'" @click="handleUp(scope.row)"
              v-hasPermi="['system:list:remove']"></el-button>
          </el-tooltip>

          <el-tooltip class="item" effect="light" :content="scope.row.isBottom != '1' ? '置底' : '取消置底'" placement="top">
            <el-button size="mini" type="primary"
              :icon="scope.row.isBottom != '1' ? 'el-icon-download' : 'el-icon-refresh-left'"
              @click="handleDown(scope.row)" v-hasPermi="['system:list:remove']"></el-button>
          </el-tooltip>
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-upload2"-->
          <!--            @click="handleUp(scope.row)"-->
          <!--          >置顶</el-button>-->
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-download"-->
          <!--            @click="handleDown(scope.row)"-->
          <!--          >置底</el-button>-->

          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-delete"-->
          <!--            @click="handleDelete(scope.row)"-->
          <!--            v-hasPermi="['bussiness:dynamic:remove']"-->
          <!--          >删除</el-button>-->
          <el-tooltip class="item" effect="light" content="删除" placement="top">
            <el-button size="mini" style="width: 22px" type="danger" icon="el-icon-delete"
              @click="handleDelete(scope.row)" v-hasPermi="['system:list:remove']"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改动态对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body v-dialog-drag
      :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="60px">
        <el-row>
          <el-col :span="16" style="border-top: 3px solid #5eacf8; padding-top: 20px; margin-right: 10px">
            <el-form-item label="标题" prop="dynamicTitle">
              <el-input v-model="form.dynamicTitle" placeholder="请输入标题" />
            </el-form-item>
            <el-form-item label="动态">
              <Ueditor v-model="form.dynamicContent" ref="ueditor"></Ueditor>
            </el-form-item>
            <!--            <el-form-item label="结果">-->
            <!--              {{getResult(form.dynamicContent)}}-->
            <!--            </el-form-item>-->
          </el-col>

          <el-col :span="1"> </el-col>

          <el-col :span="7" style="border-top: 3px solid #49a080; padding-top: 20px">
            <el-form-item label="分类" prop="dynamicTypeId">
              <el-select v-model="form.dynamicTypeId" placeholder="请选择类别" style="width: 100%">
                <el-option v-for="dict in dynamicTypeList" :key="dict.dynamicTypeId" :label="dict.dynamicTypeName"
                  :value="dict.dynamicTypeId"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="编者" prop="dynamicFeed">
              <el-input v-model="form.dynamicFeed" placeholder="请输入编者" />
            </el-form-item>
            <el-form-item label="来源" prop="dynamicSource">
              <el-input v-model="form.dynamicSource" placeholder="请输入来源" />
            </el-form-item>
            <el-form-item label="时间" prop="pushTime">
              <el-date-picker clearable v-model="form.pushTime" type="date" value-format="yyyy-MM-dd"
                placeholder="请选择发布时间" style="width: 100%">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="附件" prop="files">
              <file-upload v-model="form.files" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('0')">暂 存</el-button>
        <el-button type="primary" @click="submitForm('1')">发 布</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addDynamic,
  delDynamic,
  dynamicBack,
  getDynamic,
  moveUpOrDown,
  listDynamic,
  updateDynamic,
} from "@/api/bussiness/dynamic";
import { listDynamicType } from "@/api/bussiness/dynamicType";
import Ueditor from "@/components/Ueditor";

export default {
  name: "Dynamic",
  dicts: ["publish_status"],
  components: {
    Ueditor,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 动态表格数据
      dynamicList: [],
      dynamicTypeList: [],
      // 日期范围
      dateRange: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dynamicTypeId: null, //动态ID
        dynamicTitle: null, //动态标题
        dynamicFeed: null, //动态编者
        status: null, //状态
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        dynamicTypeId: [
          { required: true, message: "动态类别ID不能为空", trigger: "'blur', 'change'" },
        ],
        dynamicTitle: [{ required: true, message: "标题不能为空", trigger: "blur" }],
        dynamicFeed: [{ required: true, message: "编者不能为空", trigger: "blur" }],
        dynamicContent: [
          { required: true, message: "动态内容不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getTypeList();
    this.getList();
  },
  methods: {
    getTypeList() {
      var para = {
        pageNum: 1,
        pageSize: 1000,
      };
      listDynamicType(para).then((response) => {
        this.dynamicTypeList = response.rows;
      });
    },
    /** 查询动态列表 */
    getList() {
      this.loading = true;
      listDynamic(this.addDateRange(this.queryParams, this.dateRange)).then(
        (response) => {
          this.dynamicList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // readyUeditor(ueditor){
    //   console.log(ueditor);
    //   this.ueditor = ueditor;
    //   // var text1 = ueditor.getContentTxt();
    //   // console.log(text1)
    // },
    getResult(result) {
      // if(this.ueditor){
      //   var text1 = this.ueditor.getContentTxt();
      //   console.log(text1);
      //   return  this.ueditor.getContentTxt();
      // }
      // return "";
    },
    // 表单重置
    reset() {
      this.form = {
        dynamicId: null,
        dynamicTypeId: null,
        dynamicTitle: null,
        orderNum: null,
        dynamicSubTitle: null,
        dynamicFeed: null,
        dynamicRevieww: null,
        pushTime: null,
        dynamicSource: null,
        dynamicContent: null,
        files: null,
        status: null,
        isTop: null,
        isBottom: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.dynamicId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加动态";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const dynamicId = row.dynamicId || this.ids;
      getDynamic(dynamicId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改动态";
      });
    },
    handleUp(row) {
      this.reset();
      const dynamicId = row.dynamicId || this.ids;
      debugger
      console.log(row.isTop);
      if (row.isTop == '1') {
        var para = { dynamicId: dynamicId, isTop: "0", isBottom: "0" };
        updateDynamic(para).then((response) => {
          this.$modal.msgSuccess("取消置顶成功");
          this.getList();
        });
      } else {
        var para = { dynamicId: dynamicId, isTop: "1", isBottom: "0" };
        updateDynamic(para).then((response) => {
          this.$modal.msgSuccess("置顶成功");
          this.getList();
        });
      }
    },
    handleDown(row) {
      this.reset();
      const dynamicId = row.dynamicId || this.ids;
      if (row.isBottom == '1') {
        var para = { dynamicId: dynamicId, isTop: "0", isBottom: "0" };
        updateDynamic(para).then((response) => {
          this.$modal.msgSuccess("取消置底成功");
          this.getList();
        });
      } else {
        var para = { dynamicId: dynamicId, isTop: "0", isBottom: "1" };
        updateDynamic(para).then((response) => {
          this.$modal.msgSuccess("置底成功");
          this.getList();
        });
      }
    },
    /** 提交按钮 */
    submitForm(status) {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.status = status;
          if (!!this.form.dynamicContent) {
            this.form.dynamicSubTitle = this.$refs.ueditor
              .getContentText()
              .substring(0, 600);
            if (this.form.dynamicId != null) {
              updateDynamic(this.form).then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addDynamic(this.form).then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          } else {
            this.$modal.msgError("请输入动态内容！");
          }

        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const dynamicIds = row.dynamicId || this.ids;
      this.$modal
        .confirm('是否确认删除动态编号为"' + dynamicIds + '"的数据项？')
        .then(function () {
          return delDynamic(dynamicIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    handleBack(row) {
      const dynamicIds = row.dynamicId || this.ids;
      if (dynamicIds.length == 0) {
        this.$modal.msgError("请先选择动态信息！");
        return;
      }
      this.$modal
        .confirm('是否确认撤回动态编号为"' + dynamicIds + '"的数据项？')
        .then(function () {
          return dynamicBack(dynamicIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("撤回成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "bussiness/dynamic/export",
        {
          ...this.queryParams,
        },
        `dynamic_${new Date().getTime()}.xlsx`
      );
    },
    async moveUpOrDown(row, type) {
      let res = await moveUpOrDown({ dynamicId: row.dynamicId, moveType: type });
      if (res.code == 200) {
        this.getList();
        this.$modal.msgSuccess("移动成功");
      }
    },
  },
};
</script>
<!--<script type="text/javascript" charset="utf-8" src="xiumi-ue-dialog-v5.js"></script>-->
<!--<link rel='stylesheet' href='xiumi-ue-v5.css'>-->
